package com.suxii.frpclient.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Build;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class FileUtil {
    private static final String TAG = "FileUtil";


    /**
     * 从 assets 目录中读取文件并保存到指定文件
     *
     * @param context 应用上下文
     * @param assetName assets 目录中的文件名
     * @param destFile 保存的目标文件
     * @return 是否成功保存文件
     */
    public static boolean saveAssetToFile(Context context, String assetName, File destFile) {
        AssetManager assetManager = context.getAssets();
        try (InputStream in = assetManager.open(assetName);
             FileOutputStream out = new FileOutputStream(destFile)) {

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }

            Log.d(TAG, "文件已成功保存到: " + destFile.getAbsolutePath());
            return true;
        } catch (IOException e) {
            Log.e(TAG, "保存文件时发生错误: " + e.getMessage());
            return false;
        }
    }

    public static boolean formStringToFile(Context context, String src, File destFile) {
        try (FileOutputStream out = new FileOutputStream(destFile)) {
            out.write(src.getBytes(StandardCharsets.UTF_8));
            return true;
        } catch (IOException e) {
            Log.e(TAG, "保存文件时发生错误: " + e.getMessage());
            return false;
        }
    }

    public static String initConfigFile(Context context){
        String name = "frpc.toml";
        File file = new File(context.getFilesDir(), name);
        Log.i(TAG,file.getAbsolutePath());
        saveAssetToFile(context,name,file);

        return file.getAbsolutePath();
    }

    public static String saveConfigFile(Context context,String text){
        String name = "frpc.toml";
        File file = new File(context.getFilesDir(), name);
        Log.i(TAG,file.getAbsolutePath());
        formStringToFile(context,text,file);

        return  file.getAbsolutePath();
    }

    public static String getConfigContext(Context context) throws IOException {
        String name = "frpc.toml";
        File file = new File(context.getFilesDir(), name);

       try (FileInputStream fis = new FileInputStream(file)){
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
               return new String(fis.readAllBytes());
           }
           throw new IOException("读取配置文件失败");
       }catch (Exception e){
         throw new IOException("读取配置文件失败");
       }
    }

    /**
     * 在内部存储的私有目录中创建并写入文件
     *
     * @param context  上下文
     * @param fileName 文件名
     * @param content  文件内容
     * @return 是否成功写入文件
     */
    public static boolean writeToFileInternal(Context context, String fileName, String content) {
        File file = new File(context.getFilesDir(), fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(content.getBytes());
            Log.d(TAG, "文件已成功写入内部存储: " + file.getAbsolutePath());
            return true;
        } catch (IOException e) {
            Log.e(TAG, "写入文件时发生错误: " + e.getMessage());
            return false;
        }
    }

}
